package com.fenqing.comics.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author fenqing
 */
public class StrUtils {

    public static List<String> split(String str, String... regexArr) {
        if (regexArr == null) {
            return List.of(str);
        }
        String regex = regexArr[0];
        List<String> result = StrUtil.split(str, regex);
        if (regexArr.length == 1){
            return result;
        }
        return result.stream().flatMap(r -> StrUtils.split(r, ArrayUtil.sub(regexArr, 1, regexArr.length)).stream()).collect(Collectors.toList());
    }

    public static List<String> splitClean(String str, String... regexArr) {
        return CollUtil.map(CollUtil.filter(StrUtils.split(str, regexArr), StrUtil::isNotBlank), StrUtil::trim, true);
    }

    public static List<String> streamHandles(List<String> strList, List<Function<String, List<String>>> functions){
        List<String> result = strList;
        for (Function<String, List<String>> function : functions) {
            result = strList.stream().flatMap(
                        strItem -> function.apply(strItem).stream()
            ).toList();
        }
        if (!CollUtils.listContentSame(result, strList)){
            return streamHandles(result, functions);
        }
        return strList;
    }

    public static List<String> streamHandles(String strList, List<Function<String, List<String>>> functions){
        return streamHandles(List.of(strList), functions);
    }



}
